#include "logger.h"

Category* Logger::_root=nullptr;
Logger*Logger::_pInstance=nullptr;
Logger::Logger(){
    //设置日志布局
    PatternLayout *pl1=new PatternLayout();
    pl1->setConversionPattern("%d %c [%p] %m%n");
    //绑定输出的对象
    OstreamAppender* ap1=new OstreamAppender("console",&cout);
    ap1->setLayout(pl1);
    
    PatternLayout * pl2=new PatternLayout();
    pl2->setConversionPattern("%d %c [%p]  %m%n");
    FileAppender*ap2=new FileAppender("file","wd.log");
    ap2->setLayout(pl2);

    Category & root=Category::getRoot();
    root.setPriority(Priority::DEBUG);
    root.addAppender(ap1);
    root.addAppender(ap2);
    _root=&root;
}

Logger::~Logger(){
    Category::shutdown();
}

Logger*Logger::getInstance(){
    if(_pInstance==nullptr){
        _pInstance=new Logger();
    }
    return _pInstance;
}

void Logger::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance=nullptr;
    }
}
void Logger::warn(const char *file,int line,const char* func,const char *msg){
    string content="文件"+string(file)+"行号"+to_string(line)+"函数名称"+func+msg;
    _root->warn(content);
}
void Logger::error(const char *file,int line,const char *func ,const char * msg){
    string content="文件"+string(file)+"行号"+to_string(line)+"函数名称"+func+msg;
    _root->warn(content);
}
void Logger::debug(const char *file,int line,const char *func,const char*msg){
    string content="文件"+string(file)+"行号"+to_string(line)+"函数名称"+func+msg;
    _root->debug(content);
}
void Logger::info(const char *file,int line,const char *func,const char *msg){
    string content="文件"+string(file)+"行号"+to_string(line)+"函数名称"+func+msg;
    _root->debug(content);
}
void test(){

}

int main(){
    test();
    return 0;
}
